package com.lotter.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.lotter.common.context.YcContext;
import com.lotter.common.exception.LotterCusException;

/**
 * 
 * 
 * @author panguixiang
 *
 */
public class DateUtil {
	
	

   /**
    * 两个日期对比
    * @param dt1
    * @param dt2
    * @return
    */
   public static int compare_date(Date dt1, Date dt2) {
        if (dt1.getTime() > dt2.getTime()) {
            return 1;
        } else if (dt1.getTime() < dt2.getTime()) {
            return -1;
        } else {
            return 0;
        }
   }


	/**
	 * 
	 * 
	 * @param codeDate
	 * @return
	 */
	public static boolean compareCodeDateTime(Date codeDate) {
		Date after = new Date();
		boolean flag = codeDate.before(after);
		if (!flag) {
			return false;
		}
		long l = (after.getTime() - codeDate.getTime()) / 1000;
		if (l>0&&l<=60) {
			return true;
		}
		return false;
	}
	
	public static String patternDateToStr(Date date,String pattern) {
		 SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
		 return dateFormat.format(date);
	}
	
	public static Date stringToDate(String dateStr,String pattern) throws Exception{
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);                
		 Date date = sdf.parse(dateStr);
		 return date;
	}
	

	public static boolean checkSendSmsCodeTime(Date dateA) throws ParseException {
		Calendar cal = Calendar.getInstance();
		long now = cal.getTimeInMillis();
		cal.setTime(dateA);
		long lastly = cal.getTimeInMillis();
		long c = now - lastly;
		if(c<=60*1000) {//
			return true;
		} else {
			return false;
		}
	}
	

    public static String formatDateTime(long timeMillis){
		long s = timeMillis/1000;
		long sss = (timeMillis-s*1000);
		return s+"."+sss;
    }
	
    public static String date2String(Date date) throws RuntimeException {
		if (date == null) {
			return "";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(YcContext.DATE_STR_YMD);
		return sdf.format(date);
	}
    
	public static String date2String(Date date, String pattern) throws RuntimeException  {
		if (date == null) {
			return "";
		}
		if (pattern == null || pattern.equals("")) {
			pattern = YcContext.DATE_STR_YMD;			
		}
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(date);
	}
 
	public static Date string2Date(String strDate, String pattern) {
		if (strDate == null || strDate.equals("")) {
			return null;
		}
		if (pattern == null || pattern.equals("")) {
			pattern = YcContext.DATE_STR_YMD;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		Date date = null;

		try {
			date = sdf.parse(strDate);
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
		return date;
	}

	public static Date getNextDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		date = calendar.getTime();
		return date;
	}
	
	/**
	 * 日期格式转换成响应的日期类型
	 * @param pattern  格式
	 * @return 日期
	 */
	public static Date DateToPattenDate(Date date,String pattern) throws LotterCusException{
		if (date == null) {
			return null;
		}
		Date datenew = null ;
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		try {
			datenew = sdf.parse(sdf.format(date));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return datenew ;
	}
	
	/**
	 * 日期加上天数得到新的日期
	 * @param olddate
	 * @param day
	 * @return
	 */
	public static Date  getDateplusDay(Date olddate ,Integer day,Integer hour,
			Integer minute,Integer second) throws LotterCusException{
		Date newdate = null ;
		if (olddate == null) {
			throw new java.lang.RuntimeException();
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(olddate);   
		if(day  != null){
		    cal.add(Calendar.DATE,day); 
		}
		if(hour  != null){
			cal.add(Calendar.HOUR,hour); 
		}
		if(minute  != null){
			cal.add(Calendar.MINUTE,minute); 
		}
		if(second != null){
			cal.add(Calendar.SECOND,second); 
		}
		newdate=cal.getTime();
		return newdate ;
	}
	
	public static void main(String args[]) throws Exception {
	    System.out.println( DateUtil.date2String(new Date(),YcContext.PATTERN_CONNECT));
		//System.out.println(getDateplusDay(new Date(),0,1,10,20));
	}

	
}
